package com.arcblock.demo


import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.http.SslError
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
/**
 * webview加载容器
 */
class WebViewActivity : AppCompatActivity() {
    class JsObject(var ctx: Context) {
        //暴露给js的类
        @JavascriptInterface
        //js访问该类的方法
        fun call(n:String)
        {
            var intent= Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:" + n));
            startActivity(ctx,intent,null);
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //去除title
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        var web: WebView = WebView(this)
        setContentView(web)
        val webSettings: WebSettings = web.getSettings()
        //解决https白屏问题
        webSettings.domStorageEnabled = true
        web.setWebChromeClient(WebChromeClient())
        web.webViewClient = object  : WebViewClient(){
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                //必须要加入，不然出现白屏
                web.loadUrl(url);
                return true;
            }

            override fun onReceivedSslError(view: WebView?,  handler: SslErrorHandler?,  error: SslError?) {
                if (handler != null) {
                    //接受所有证书
                    handler.proceed()
                }
            }
        }
        //设置WebView允许它执行js代码
        web.settings.setJavaScriptEnabled(true);
        var url :String = intent.getStringExtra("url")
        web.loadUrl(url)
        //暴露给js一个对象app，
        web.addJavascriptInterface(JsObject(this),"app")
    }
}